// 算法1：
// 最弱智的方式:

let alg_1 = {

    // 评分来自 https://kimlongli.github.io/2016/12/14/如何设计一个还可以的五子棋AI/
    patterns_ai: [
        {key: /22222/, value: 50000},
        {key: /022220/, value: 4320},
        {key: /022200/, value: 720},
        {key: /002220/, value: 720},
        {key: /022020/, value: 720},
        {key: /020220/, value: 720},
        {key: /22220/, value: 720},
        {key: /02222/, value: 720},
        {key: /22022/, value: 720},
        {key: /20222/, value: 720},
        {key: /22202/, value: 720},
        {key: /002200/, value: 120},
        {key: /002020/, value: 120},
        {key: /020200/, value: 120},
        {key: /000200/, value: 20},
        {key: /002000/, value: 20},
    ],

    /**
     * 选取每个空位置,计算每个空位置的权重
     * @param board
     */
    ai_choose(board) {
        let emptyCells = board.emptyCells();
        if (emptyCells.length < 0) {
            console.log('没有位置,可能是平局或者错误!');
            return;
        }

        let maxValue = -Infinity;
        let dstCell;

        // 计算每个位置的权重,取最大值
        emptyCells.forEach(cell => {
            cell.value = 2;
            let ai_score = this.evaluate(this.patterns_ai, board, cell);
            cell.value = 0;

            if (ai_score > maxValue) {
                maxValue = ai_score;
                dstCell = cell;
            }
        });

        console.log('maxValue:', maxValue);
        return dstCell;
    },

    evaluate(patterns, board, cell) {
        let cells = board.cells;

        // 横着
        let row = cells[cell.rowIdx];
        let minColIdx = clamp(cell.colIdx - 4, 0, Size - 1);
        let maxColIdx = clamp(cell.colIdx + 4, 0, Size - 1);
        let charArr = [];
        for (let j = minColIdx; j <= maxColIdx; ++j) {
            charArr.push(row[j].value);
        }
        let rowStr = charArr.join('');


        // 竖着
        let minRowIdx = clamp(cell.rowIdx - 4, 0, Size - 1);
        let maxRowIdx = clamp(cell.rowIdx + 4, 0, Size - 1);
        charArr.length = 0;
        for (let i = minRowIdx; i <= maxRowIdx; ++i) {
            charArr.push(cells[i][cell.colIdx].value);
        }
        let colStr = charArr.join('');

        //  \
        // 直接先计算所有的,再循环剔除掉越界的
        charArr.length = 0;
        for (let k = 0; k < 9; ++k) {
            let r = cell.rowIdx - 4 + k;
            let c = cell.colIdx - 4 + k;
            if (r >= 0 && r < Size && c >= 0 && c < Size) {
                charArr.push(cells[r][c].value);
            }
        }
        let fxStr = charArr.join('');

        // /
        charArr.length = 0;
        for (let k = 0; k < 9; ++k) {
            let r = cell.rowIdx + 4 - k;
            let c = cell.colIdx - 4 + k;
            if (r >= 0 && r < Size && c >= 0 && c < Size) {
                charArr.push(cells[r][c].value);
            }
        }
        let xStr = charArr.join('');

        // 计算 得分
        return this._callScore(patterns, [rowStr, colStr, fxStr, xStr]);
    },
    _callScore(patterns, strArr) {
        let sum = 0;
        strArr.forEach(str => {
            let score = 0;
            for (let i = 0, len = patterns.length; i < len; ++i) {
                let p = patterns[i];
                let key = p.key;
                if (key.test(str)) {
                    score = p.value;
                    break;
                }
            }
            sum += score;
        });
        return sum;
    }
};
